package v1

import (
	"pq/models"
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type SlTestLibController struct {
	TokenCheckController
}

func (sc *SlTestLibController) Get(){
	limit,_ := sc.GetInt("limit",0)
	skip,_  := sc.GetInt("skip",models.MAXROWS)
	cid,_ := sc.GetInt("category_id",1)
	sllib := new(models.PqSlTestLibrary)
	var cate models.PqTestCategory
	slave := models.GetSlaveDb()
	_ = slave.QueryTable("pq_test_category").Filter("id", cid).One(&cate)
	sls,count := sllib.ListAll(cate.Lft,cate.Rgt,skip,limit)
	sc.Response(sls,count)
}
func (sc *SlTestLibController) Post()  {
	row := new(models.PqSlTestLibrary)
	row.Id,_ = sc.GetInt("id",0)
	row.Title = sc.GetString("title")
	row.Note  = sc.GetString("note")
	row.HardRate,_ = sc.GetFloat("hard_rate",1.0)
	row.State,_ = sc.GetInt("state",1)
	row.PqTestCategoryId,_ = sc.GetInt("pq_test_category_id")
	row.AreaCode = sc.GetString("area_code","")
	row.AreaCodes = sc.GetString("area_codes","")
	master := models.GetMasterDb()

	if row.Id > 0 {
		_,err := master.Update(row)
		beego.Error(err)
	}else{
		row.Id = 0
		ins, err := master.Insert(row)
		beego.Warning(err)
		row.Id = int(ins)
	}
	res := map[string]interface{}{}
	res["id"] = row.Id
	sc.Single(res)
}
func (sc *SlTestLibController) Del(){
	id := sc.Ctx.Input.Param(":id")
	master := models.GetMasterDb()
	if _,err := master.QueryTable("pq_sl_test_library").Filter("id",id).Update(orm.Params{
		"state" : -1,
	});err != nil{
		sc.Fail(err.Error(),"")
		return
	}
	sc.Success("Deleted Successfully")
}